package com.ho1ho.springboot.framework.core.interceptors;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MaintenanceInterceptor implements HandlerInterceptor {
	@Value("${system.maintenance.enable}")
	private boolean isMaintenanceEnabled = false;

	@Value("${system.maintenance.start.time}")
	private int maintenanceStartTime = -1;

	@Value("${system.maintenance.end.time}")
	private int maintenanceEndTime = -1;

	@Value("${system.maintenance.url}")
	private String maintenanceUrl;

	private static final File externalFile = new File("./config.cfg");

	private static final Map<String, Object> cfgMap = new HashMap<>();

	// before the actual handler will be executed
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// log.trace("MaintenanceInterceptor preHandle");
		isMaintenanceEnabled = false;
		maintenanceStartTime = -1;
		maintenanceEndTime = -1;
		maintenanceUrl = "";

		try {
			List<String> cfgVal = FileUtils.readLines(externalFile, "UTF-8");
			for (String kv : cfgVal) {
				cfgMap.put(kv.split("=")[0], kv.split("=")[1]);
			}

			boolean tmpMaintenanceEnabled = Boolean.valueOf(cfgMap.get("system.maintenance.enable").toString());
			if (tmpMaintenanceEnabled) {
				log.info("========== External Maintenance ==========");

				isMaintenanceEnabled = tmpMaintenanceEnabled;
				maintenanceStartTime = Integer.valueOf(cfgMap.get("system.maintenance.start.time").toString());
				maintenanceEndTime = Integer.valueOf(cfgMap.get("system.maintenance.end.time").toString());
				maintenanceUrl = cfgMap.get("system.maintenance.url").toString();
			}
		} catch (IOException ioe) {
			// log.warn("===== External config file does not exist. =====");
		}

		if (isMaintenanceEnabled) {
			Calendar cal = Calendar.getInstance();
			int hour = cal.get(Calendar.HOUR_OF_DAY);

			if (hour >= maintenanceStartTime && hour <= maintenanceEndTime) {
				log.info("========== Maintenance Information ========== {} {} {}", maintenanceStartTime, maintenanceEndTime, maintenanceUrl);
				// Maintenance time, send to maintenance page
				response.sendRedirect(maintenanceUrl);
				return false;
			} else {
				return true;
			}
		}
		return true;
	}
	//
	// @Override
	// public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView
	// modelAndView) throws Exception {
	// log.trace("MaintenanceInterceptor postHandle");
	// }
	//
	// @Override
	// public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
	// throws Exception {
	// log.trace("MaintenanceInterceptor afterCompletion");
	// }
}